package com.qbkj.base_lib.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.qbkj.base_lib.utils.ViewController;
import com.qbkj.base_lib.utils.TagUtils;


/**
 * Created by Administrator on 2017/8/15 0015.
 */

public abstract class BaseFragment extends Fragment {
    protected View mView;
    protected Context mContext;
    protected Intent mIntent;
    protected ViewController g;
    public String title;
    protected Bundle mBundle;

    protected abstract int onCreateLayout();

    protected abstract void init();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(onCreateLayout(), container, false);
        init();
        return mView;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getContext();
        mIntent = new Intent();
        mBundle = new Bundle();
        g = new ViewController(mContext, mView);

    }

    private boolean hidden;

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        this.hidden = hidden;
    }

    //获取当前fragment是否可见状态
    public boolean getVisibleHint() {
        if (!hidden) return true;
        else return false;
    }


    protected void skipActivity(Class cls, int ResultCode) {
        skipActivity(cls, ResultCode, mIntent, mBundle);
    }

    public void skipActivity(Class cls) {
        skipActivity(cls, TagUtils.NORESULTCODE, mIntent, mBundle);
    }

    protected void skipActivityFinish(Class cls) {
        skipActivity(cls);
        getActivity().finish();
    }

    protected void skipActivityFinish(Class cls, int ResultCode) {
        skipActivity(cls, ResultCode);
        getActivity().finish();
    }

    //跳转
    public void skipActivity(Class cls, int ResultCode, Intent intent, Bundle bundle) {
        intent.setClass(getActivity(), cls);
        if (bundle != null) intent.putExtras(bundle);
        if (TagUtils.NORESULTCODE != ResultCode) {
            getActivity().startActivityForResult(intent, ResultCode);
        } else {
            getActivity().startActivity(intent);
        }
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }


}
